package cn.michael.dev.data.structure.queue;

/**
 * Created by hufenggang on 2021/8/25.
 * <p>
 * 基于数组实现单链表
 */
public class QueueByArray {
    // 数组: items, 数组大小: n
    private String[] items;
    private int n = 0;
    // head表示队头下标，tail表示队尾下标
    private int head = 0;
    private int tail = 0;

    public QueueByArray(int capacity) {
        items = new String[capacity];
        n = capacity;
    }

    /**
     * 入队
     *
     * @param item
     * @return
     */
    public boolean enqueue(String item) {
        if (tail == n) {
            return false;
        }

        items[tail] = item;
        ++tail;
        return true;
    }

    /**
     * 出队
     *
     * @return
     */
    public String dequeue() {
        if (head == tail) {
            return null;
        }

        String ret = items[head];
        ++head;
        return ret;
    }
}
